$(function(){


    easyloader.base = '../../js/tools/locale/';
    easyloader.load('easyui-lang-zh_CN.js', function(){
        var dateTime = getDateTime();
        var pram = {
            title: '',
            currentPage: 1,
            startDate: dateTime[0],
            endDate: dateTime[1],
			flag:'1'
        };
        $("#startdate").val(dateTime[0]);
        $("#enddate").val(dateTime[1]);
        $("#data").datagrid({
            height: 450,
			width:1140,
            pageList: [15, 20, 25, 30],
            pageSize: 15,
            showPageList: true,
            pagination: true,
            rownumbers: true,
            striped: true,
            singleSelect: true,
            queryParams: pram,
            url: '../../groupbyajax/groupby!getCategoryList.action',
            onLoadSuccess: function(data){
                var myJSONText = JSON.stringify(data, function(key, value){
                    return value;
                });
                
            },
            columns: [[{
                field: 'checkbox',
                title: '<input type="checkbox" name="all" onclick="selectAll(this)">',
                width: 30
            }, {
                field: 'title',
                title: '名称',
                width: 300,
                align: 'left'
            }, {
                field: 'delete',
                title: '删除',
                width: 80,
                align: 'center'
            }]]
        });
        addButton();
    });
    
   $("#dialog").window({
        closed: true,
        modal: true,
		height: 95,
		width:220,
		left:"40%"
    });
    $("#addbut").click(function(){
        $("input[name='method']").val("addCategory");
        $("#dialog").window("open");
    });
});

function addButton(){
    var pager = $('#data').datagrid('getPager');
    pager.pagination({
        pageList: [15, 20, 25, 30],
        pageSize: 15,
        showPageList: true,
        buttons: [{
            iconCls: 'icon-print',
            handler: function(){
                doPrint();
            }
        }]
    });
}

function search(){
    var title = $.trim($('#title').val());
    var startdate = $.trim($("#startdate").val());
    var enddate = $.trim($("#enddate").val());
    $('#data').datagrid({
        striped: true,
        queryParams: {
            title: title,
            startDate: startdate,
            endDate: enddate
        }
    });
}

function deleteCategory(id){
    if (id == "all") {
        id = getSelectIds();
    }
    
    $.messager.confirm('删除', '你确定要删除记录吗?', function(r){
		if(r)
		{
			$.post("../../groupbyajax/groupby!deleteCategory.action", {
            id: id
        }, function(data){
            if (data.success) {
                $("#data").datagrid('reload');
                $.messager.alert("消息框", "删除成功！");
            }
            else {
                $.messager.alert("消息框", "删除失败！");
            }
        });
		}
        
    });
    
}

function selectAll(obj){
    if ($(obj).attr("checked")) {
    
        $("#data tbody tr").find('td:eq(0) div input').each(function(){
            $(this).attr("checked", true);
        });
    }
    else {
        $("#data tbody tr").find('td:eq(0) div input').each(function(){
            $(this).attr("checked", false);
        });
    }
}

function getSelectIds(){
    var ids = "";
    $("#data tbody tr").find('td:eq(0) div input').each(function(){
    
        if ($(this).attr("checked")) {
            ids += $(this).val() + ",";
        }
    });
    ids = ids.substring(0, ids.length - 1);
    return ids;
}

function save(){

    var categoryName = $.trim($("#categoryName").val());
	var flag=$("input[name='flag']").val();
    if (categoryName != "") {
		var method=$("input[name='method']").val();
		var pram=""
		if(method=="editCategory")
		{
			var id=$("input[name='id']").val();
			pram={title: "",id:id,categoryName: categoryName,currentPage: 1,flag:flag};
		}else
		{
			pram={title: "",categoryName: categoryName,currentPage: 1,flag:flag};
		}

        $.getJSON("../../groupbyajax/groupby!"+method+".action", pram, function(data){
            if (data) {
                $.messager.alert('消息框', "添加成功！", 'info', function(){
                    $("#dialog").window("closed");
                    $("#data").datagrid('reload');
                    $("#dialog").form("clear");
                });
            }
            else {
                $.messager.alert('消息框', "修改失败! ", 'info', function(){
                    $("#dialog").window("closed");
                });
            }
        });
    }else
	{
		 $.messager.alert('消息框', "类型名称不能为空! ");
	} 
}
function modify(id,name)
{
	 $("input[name='method']").val("editCategory");
	 $("input[name='id']").val(id);
	 $("input[name='categoryName']").val(name);
     $("#dialog").window("open");
}
